﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.MT3DMS.DomainLayer;
using System.ComponentModel;
namespace HA.ModFlow.Controls.MT3DMSWindows
{
    /// <summary>
    /// winSSMixingPack.xaml 的交互逻辑
    /// </summary>
    public partial class winSSMixingPack : Window
    {
        public winSSMixingPack(SourceSinkMixing ssm,HA.Data.Grid3dCollecion grid)
        {
            InitializeComponent();



            this._ssMixing = new PCSSMixing(ssm);




            


            this.BuildColumns();


            this.DataContext = this._ssMixing;

            this._grid = grid;


            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dtGrid.ContextMenu = cm;

        }
        HA.Data.Grid3dCollecion _grid;

        PCSSMixing _ssMixing;

        public PCSSMixing SsMixing
        {
            get { return _ssMixing; }
            set { _ssMixing = value; }
        }

      

        protected void BuildColumns()
        {
            DataGridTextColumn text = new DataGridTextColumn();
            text.Header = "行";
            Binding bin = new Binding("Mi.Row");
            text.Binding = bin;
            this.dtGrid.Columns.Add(text);

            text = new DataGridTextColumn();
            text.Header = "列";
            bin = new Binding("Mi.Col");
            text.Binding = bin;
            this.dtGrid.Columns.Add(text);

            text = new DataGridTextColumn();
            text.Header = "层";
            bin = new Binding("Mi.Layer");
            text.Binding = bin;
            this.dtGrid.Columns.Add(text);



       


            List<string> arrystr = new List<string>();
            arrystr.Add("井");
            arrystr.Add("定水头");
            arrystr.Add("河流");
            arrystr.Add("通用水头");
            arrystr.Add("给定浓度");
            arrystr.Add("质量负载");       


            DataGridComboBoxColumn cbo = new DataGridComboBoxColumn();
            cbo.Header = "类 型";
            bin = new Binding("Mi.SsType");
            bin.Converter = new ConvertType();
            cbo.SelectedItemBinding = bin;
            
            cbo.ItemsSource = arrystr;
            this.dtGrid.Columns.Add(cbo);


            List<string> species = this._ssMixing.SsMixing.CurrentSpecies;
          
            int i=0;
            foreach (string item in species)
            {
                DataGridTextColumn spcol = new DataGridTextColumn();
                spcol.Header = item;

                string propName = "SpeciesValue[" + i.ToString() + "]";


                Binding bind = new Binding(propName);
                spcol.Binding = bind;
                this.dtGrid.Columns.Add(spcol);

                i++;
                
            }
            


 
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
          
        }
  

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

            this._ssMixing.CreateNew();

        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            int a = this.dtGrid.SelectedIndex;
            List<PCMixing> arry = new List<PCMixing>();
            for (int i = 0; i < this.dtGrid.SelectedItems.Count; i++)
            {
                arry.Add(this.dtGrid.SelectedItems[i] as PCMixing);             
            }
            //if (a!=-1)
            //{
            //    this._ssMixing.Items.RemoveAt(a);
                
            //}

            foreach (PCMixing item in arry)
            {
                this._ssMixing.Items.Remove(item);
            }

            //this.dtGrid.Items.Refresh();


        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {

            if (this._ssMixing.CheckInputIJKorIndex(this._grid.MainGrid.ModFlowGrid.RowsCount, this._grid.MainGrid.ModFlowGrid.ColsCount, this._grid.MainGrid.ModFlowGrid.LayersCount))
            {

            }
            else
            {
                return;
            }

            this._ssMixing.Confirm();
            this.DialogResult = true;





            this.Close();
        }

        private void dtGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
    }

    public class ConvertType : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MixingSSType type = (MixingSSType)value;
            string str="";
            switch (type)
            {
                case MixingSSType.WELL:
                    str = "井";
                    break;
                case MixingSSType.CONHEAD:
                    str = "定水头";
                    break;
                case MixingSSType.RIVER:
                    str = "河流";
                    break;
                case MixingSSType.GENHEAD :
                    str = "通用水头";
                    break;
                case  MixingSSType.SECCONC:
                    str = "给定浓度";
                    break;
                case MixingSSType.MASSLOAD:
                    str = "质量负载";
                    break;
            }
            return str ;



            // arrystr.Add("井");
            //arrystr.Add("定水头");
            //arrystr.Add("河流");
            //arrystr.Add("通用水头");
            //arrystr.Add("给定浓度");
            //arrystr.Add("质量负载");  

            //arrystr.Add("WELL");
            //arrystr.Add("CONHEAD");
            //arrystr.Add("RIVER");
            //arrystr.Add("GENHEAD");
            //arrystr.Add("SECCONC");
            //arrystr.Add("MASSLOAD"); 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MixingSSType type = MixingSSType.SECCONC;
            string str = (string )value;
            switch (str)
            {
                case "井":
                    type = MixingSSType.WELL;
                    break;
                case  "定水头":
                    type = MixingSSType.CONHEAD;
                    break;
                case "河流":
                    type = MixingSSType.RIVER;
                    break;
                case "通用水头":
                    type = MixingSSType.GENHEAD;
                    break;
                case  "给定浓度":
                    type = MixingSSType.SECCONC;
                    break;
                case "质量负载":
                  type = MixingSSType.MASSLOAD;;
                    break;
            }
            return type;
        }
    }

}
